/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: SearchController.java 30 2012-08-28 07:55:17Z zhuxiaopeng $
 * $Revision: 30 $
 * $Date: 2012-08-28 15:55:17 +0800 (二, 2012-08-28) $
 * $Author: zhuxiaopeng $
 */
package daily.web.search;

import static zhu.framework.web.WebUtility.*;

import java.util.*;

import javax.ejb.*;
import javax.enterprise.context.*;
import javax.inject.*;

import zhu.framework.web.*;
import zhu.framework.web.jsf.*;
import daily.service.*;
import daily.util.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 30 $ - $Date: 2012-08-28 15:55:17 +0800 (二, 2012-08-28) $
 */
@Named
@RequestScoped
public class SearchController extends ConversationalController {

	public String search() {
		final Date startDate = searchConversation.getStartDate();
		final Date endDate = searchConversation.getEndDate();
		if (checkDate(startDate, endDate) == false) {
			searchConversation.setDailyList(null);
			return null;
		}
		final List<DailyData> dailyList = dailyService.getDailyByPeriod(searchConversation.getSelectStaff(), startDate, endDate);
		searchConversation.setDailyList(dailyList);
		return null;
	}

	private boolean checkDate(final Date startDate, final Date endDate) {
		if (startDate == null || endDate == null) {
			if (startDate == null) {
				getWebRequestContext().addMessage(new WebMessage("開始日を入力してください。"));
			}
			if (endDate == null) {
				getWebRequestContext().addMessage(new WebMessage("終了日を入力してください。"));
			}
			return false;
		} else {
			if (DateUtils.compareIgnoreTime(startDate, endDate) > 0) {
				getWebRequestContext().addMessage(new WebMessage("開始日は終了日前の日付を入力してください。"));
				return false;
			}
		}
		return true;
	}

	@EJB
	private DailyServiceImpl dailyService;

	@Inject
	private SearchConversation searchConversation;
}
